package com.it.homework;

/**
 * @author げっしょく
 * @date 2021/8/7
 */
public class T3Role extends Thread {
    private T3Desk o1;

    public T3Role(T3Desk o1) {
        this.o1 = o1;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (o1.getObj()) {
                if (o1.getCount() == 0) {
                    return;
                } else {
                    if (o1.isFlag()) {
                        System.out.println("吃货再吃汉堡包");
                        int i = o1.getCount() - 1;
                        o1.setCount(i);
                        System.out.println(o1.getCount());
                        o1.setFlag(false);
                        o1.getObj().notify();
                    }else {
                        try {
                            o1.getObj().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}


class T3Role2 extends Thread {
    private T3Desk o1;

    public T3Role2(T3Desk o1) {
        this.o1 = o1;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (o1.getObj()) {
                if (o1.getCount() == 0) {
                    return;
                } else {
                    if (o1.isFlag()) {
                        try {
                            o1.getObj().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("厨师正在做汉堡包");
                        o1.setFlag(true);
                        o1.getObj().notify();
                    }
                }
            }
        }
    }
}
